#include <GL/glew.h>
#include <GL/freeglut.h>
#include "window.h"
#include "Lamp.h"
#include "Model.h"

const int PERIOD = 1000 / 60;

int window_width = 1760;
int window_height = 1100;

Window * window;
//Lamp * lamp;
Model * model;


void DisplayFunc()
{
	//glViewport(0, 0, 1024, 600);
	//glClearColor(0, 0, 0, 0);
	//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	window->DisplayFunc();
	//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	//glEnable(GL_DEPTH_TEST);

	//glEnable(GL_LIGHTING); //Enable lighting
	//glEnable(GL_LIGHT0); //Enable light #0
	//glEnable(GL_NORMALIZE); //Automatically normalize normals
	//glShadeModel(GL_SMOOTH); //Enable smooth shading

	////picture->Display();
	//glMatrixMode(GL_PROJECTION);
	//glLoadIdentity();
	//glOrtho(-10,100, -10, 10, 1, 10);
	//glMatrixMode(GL_MODELVIEW);
	//glLoadIdentity();
	//
	////glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
	////glPushMatrix();
	////glUseProgram(0);
	////glBindFramebuffer(GL_FRAMEBUFFER, 0);
	////glDisable(GL_LIGHTING);
	////glDisable(GL_DEPTH_TEST);

	//glTranslated(0,0, -1.5);
	//glutSolidSphere(.5, 20, 20);
	////lamp->Display();
	////model->Display();
	////glPopMatrix();

	//glutSwapBuffers();
}

void KeyboardFunc(unsigned char c, int x, int y)
{
	window->KeyboardFunc(c, x, y);
}

void MouseFunc(int button, int state, int x, int y)
{
	window->MouseFunc(button, state, x, y);
}

void MotionFunc(int x, int y)
{
	window->MotionFunc(x, y);
}

void ReshapeFunc(int w, int h)
{
	window->ReshapeFunc(w, h);
}

void TimerFunc(int value)
{
	window->TimerFunc(value);
}

int main(int argc, char * argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
	glutInitWindowPosition(0, 0);
	glutInitWindowSize(window_width, window_height);
	glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
	glutCreateWindow("Still Life");

	//Testing
	//lamp = new Lamp();
	model = new Model("box.obj");
	
	if (glewInit() != GLEW_OK)
	{
		return 0;
	}

	ilInit();

	window = new Window(window_width, window_height);
	
	// this encapsulates Shader::Initialize()
	if (!window->Initialize())
	{
		return 0;
	}

	//Testing lamp
	//lamp->PostGLInitialize();
	
	// set up GL callback functions
	glutDisplayFunc(DisplayFunc); 
	glutKeyboardFunc(KeyboardFunc);
	glutMouseFunc(MouseFunc);
	glutMotionFunc(MotionFunc);
	glutReshapeFunc(ReshapeFunc);
	glutTimerFunc(PERIOD, TimerFunc, 0);

	glutMainLoop();

	return 0;
}